﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;

namespace BuildnDeploy {
    internal class SettingsViewModel : INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged;

        Settings _settings;
        public SettingsViewModel(Settings s) {
            _settings = s;
            SelectFilenameCommand = new DelegateCommand(SelectFilename);

        }

        //string _buildCommandStr = null;
        public string BuildCommandStr {
            get { return _settings.BuildCommand; }
            set {
                if (_settings.BuildCommand == value) return;
                _settings.BuildCommand = value;
                RaisePropertyChanged("BuildCommandStr");
            }
        }
        //string _deployCommandStr = null;
        public string DeployCommandStr {
            get { return _settings.DeployCommand; }
            set {
                if (_settings.DeployCommand == value) return;
                _settings.DeployCommand = value;
                RaisePropertyChanged("DeployCommandStr");
            }
        }

        public ICommand SelectFilenameCommand { get; set; }
        private void SelectFilename(object parameter) {
            if (parameter == null) return;
            OpenFileDialog dlg = new OpenFileDialog();

            //dlg.DefaultExt = ".cmd";
            dlg.Filter = "Executables and Scripts (*.cmd,*.bat,*.exe)|*.cmd;*.bat;*.exe|All files (*.*)|*.*";

            bool? result = dlg.ShowDialog();

            if (result == true) {
                if (parameter.ToString() == "build") BuildCommandStr = dlg.FileName;
                else DeployCommandStr = dlg.FileName;
            }
        }

        private void RaisePropertyChanged(string propertyName) {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
